package codepulse.blogmoon.cn.system.controller.admin;


import codepulse.blogmoon.cn.common.entity.Result;
import codepulse.blogmoon.cn.common.utils.PageUtil;
import codepulse.blogmoon.cn.system.dto.admin.QueryAdminRoleReq;
import codepulse.blogmoon.cn.system.dto.admin.RoleMenuRelDTO;
import codepulse.blogmoon.cn.system.entity.SysAdminRole;
import codepulse.blogmoon.cn.system.service.SysAdminRoleService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Resource
    private SysAdminRoleService sysAdminRoleService;

    @GetMapping("/list")
    public Result list() {
        return Result.success(sysAdminRoleService.list());
    }

    @PostMapping("/page")
    public Result page(@RequestBody QueryAdminRoleReq req) {
        Page<SysAdminRole> page = sysAdminRoleService.page(new Page<>(req.getPageNo(), req.getPageSize()), Wrappers.<SysAdminRole>lambdaQuery()
                .eq(StringUtils.isNotBlank(req.getRoleName()), SysAdminRole::getRoleName, req.getRoleName()));
        return Result.success(PageUtil.getPage(page, SysAdminRole.class));
    }

    @PostMapping("/create")
    public Result create(@RequestBody SysAdminRole sysAdminRole) {
        sysAdminRole.setCreateField();
        sysAdminRoleService.save(sysAdminRole);
        return Result.success();
    }

    @PostMapping("/modify")
    public Result modify(@RequestBody SysAdminRole sysAdminRole) {
        sysAdminRole.setModifyField();
        sysAdminRoleService.updateById(sysAdminRole);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Long id) {

        sysAdminRoleService.removeRole(id);
        return Result.success();
    }

    @GetMapping("/menuIds/{id}")
    public Result getMenuIdByRoleId(@PathVariable("id") Long roleId) {
        return Result.success(sysAdminRoleService.getMenuIdByRoleId(roleId));
    }

    @GetMapping("detail/{id}")
    public Result detail(@PathVariable("id") Long id) {
        return Result.success(sysAdminRoleService.getById(id));
    }

    @PostMapping("/saveRoleRel")
    public Result saveMenuRel(@RequestBody RoleMenuRelDTO dto){
        sysAdminRoleService.saveMenuRel(dto);
        return Result.success();
    }
}
